$(function() {
	// 初始化区域树
	initDivisionTree();
	// 初始化未绑定区域的网格列表
	// initGridUnbindDivision();
	// 初始化组织树
	initHierarchyTree();
	// 初始化未绑定组织的用户信息
	// initUserUnbindHierarchy();
});

// =================================用户网格解除绑定================================

$("#userGridUnbind").click(function() {
	var gridIds = '';
	var userIds = '';
	var unbindUserIds = '';
	var bindUserIds = '';
	var unbindGridIds = '';
	var bindGridIds = '';
	// 获取所有勾选的网格id
	$(".unbindGridSelect").each(function() {
		if ($(this).prop("checked") == true) {

			unbindGridIds += $(this).attr("ownId") + ",";
		}
	});

	$(".bindGridSelect").each(function() {
		if ($(this).prop("checked") == true) {
			bindGridIds += $(this).attr("ownId") + ",";
		}
	});

	gridIds = bindGridIds + unbindGridIds;

	if (gridIds == null || '' == gridIds) {
		message("请选择要解除绑定的网格");
		return false;
	}
	// 获取所有勾选的用户id
	// var unbindUserSelectChecked=$(".unbindUserSelect:checked");

	$(".unbindUserSelect").each(function() {
		if ($(this).prop("checked") == true) {

			unbindUserIds += $(this).attr("ownId") + ",";
		}
	});

	$(".bindUserSelect").each(function() {
		if ($(this).prop("checked") == true) {
			bindUserIds += $(this).attr("ownId") + ",";
		}
	});

	userIds = unbindUserIds + bindUserIds;
	if (userIds == '' || null == userIds) {
		message("请选择要解除绑定的用户");
		return false;
	}
	$("#progressbar").show();
	$("#right").mLoading("show");
	$.ajax({
		url : rootPath + "/ui/userGrid/unbind/" + userIds + "/" + gridIds,
		dataType : 'json',
		type : 'get',
		success : function(result) {
			if (result.code == 1) {
				$("#progressbar").hide();
				message(result.data.msg);
				// window.location.href = rootPath + '/ui/userGrid';
				serverUserUnbindHierarchyData.key = $("#hierarchyId").val();
				serverUserUnbindHierarchyTable.draw();

				serverUserBindHierarchyData.key = $("#hierarchyId").val();
				serverUserBindHierarchyTable.draw();

				serverGridUnbindDivisionData.key = $("#gridAboutD").val();
				serverGridUnbindDivisionTable.draw();

				serverGridBindDivisionData.key = $("#gridAboutD").val();
				serverGridBindDivisionTable.draw();
				
				$("#selectAllBindUser").prop("checked",false);
				$("#selectAllUnbindUser").prop("checked",false);
				$("#selectAllUnbindGrid").prop("checked",false);
				$("#selectAllbindGrid").prop("checked",false);
				
			}
			$("#right").mLoading("hide");
		}
	});

});
// =================================用户网格绑定================================
$("#userGridBind").click(function() {
	var gridIds = '';
	var userIds = '';
	var unbindUserIds = '';
	var bindUserIds = '';
	var unbindGridIds = '';
	var bindGridIds = '';
	// 获取所有勾选的网格id
	$(".unbindGridSelect").each(function() {
		if ($(this).prop("checked") == true) {

			unbindGridIds += $(this).attr("ownId") + ",";
		}
	});

	$(".bindGridSelect").each(function() {
		if ($(this).prop("checked") == true) {
			bindGridIds += $(this).attr("ownId") + ",";
		}
	});

	gridIds = bindGridIds + unbindGridIds;

	if (gridIds == null || '' == gridIds) {
		message("请选择要绑定的网格");
		return false;
	}
	// 获取所有勾选的用户id
	// var unbindUserSelectChecked=$(".unbindUserSelect:checked");

	$(".unbindUserSelect").each(function() {
		if ($(this).prop("checked") == true) {

			unbindUserIds += $(this).attr("ownId") + ",";
		}
	});

	$(".bindUserSelect").each(function() {
		if ($(this).prop("checked") == true) {
			bindUserIds += $(this).attr("ownId") + ",";
		}
	});

	userIds = unbindUserIds + bindUserIds;
	if (userIds == '' || null == userIds) {
		message("请选择要绑定的用户");
		return false;
	}
	$("#progressbar").show();
	$("#right").mLoading("show");
	$.ajax({
		url : rootPath + "/ui/userGrid/bind/" + userIds + "/" + gridIds,
		dataType : 'json',
		type : 'get',
		success : function(result) {
			if (result.code == 1) {
				$("#progressbar").hide();
				message(result.data.msg);
				// window.location.href = rootPath + '/ui/userGrid';
				serverUserUnbindHierarchyData.key = $("#hierarchyId").val();
				serverUserUnbindHierarchyTable.draw();

				serverUserBindHierarchyData.key = $("#hierarchyId").val();
				serverUserBindHierarchyTable.draw();

				serverGridUnbindDivisionData.key = $("#gridAboutD").val();
				serverGridUnbindDivisionTable.draw();

				serverGridBindDivisionData.key = $("#gridAboutD").val();
				serverGridBindDivisionTable.draw();
				
				$("#selectAllBindUser").prop("checked",false);
				$("#selectAllUnbindUser").prop("checked",false);
				$("#selectAllUnbindGrid").prop("checked",false);
				$("#selectAllbindGrid").prop("checked",false);
				
			}
			$("#right").mLoading("hide");
		}
	});

});
// ===============================未绑定组织的用户列表=============================
$(".searchBtn3").click(function() {
	serverUserUnbindHierarchyData.con = $("#search3").val();
	var val = $("#hierarchyId").val();
	if (val == '') {
		val = '0';
	}
	serverUserUnbindHierarchyData.key = val;
	serverUserUnbindHierarchyTable.draw();
});
$(".resetBtn3").click(function() {
	$("#search3").val('');
	var val = $("#hierarchyId").val();
	if (val == '') {
		val = '0';
	}
	serverUserUnbindHierarchyData.key = val;
	serverUserUnbindHierarchyData.con = '';
	serverUserUnbindHierarchyTable.draw();
});
var serverUserUnbindHierarchyData = {};
var serverUserUnbindHierarchyTable;

function initUserUnbindHierarchy(id) {

	// 未绑定网格按钮全选
	$("#selectAllUnbindUser").click(function() {
		var state = $(this).prop("checked");
		$(".unbindUserSelect").prop("checked", state);
	});

	serverUserUnbindHierarchyTable = $('#userUnbindHierarchyTable')
			.DataTable(
					{
						"serverSide" : true,
						"ordering" : false,
						"ajax" : {
							"dataType" : "json",
							"type" : "post",
							"cache" : false,
							"url" : window.rootPath
									+ '/ui/userGrid/showUserUnbindHierarchyPage',
							"data" : function(d) {
								return $.extend({}, d,
										serverUserUnbindHierarchyData);
							}
						},
						columns : [
								{
									data : null,
									defaultContent : ""
								},
								{
									data : 'id',
									defaultContent : "",
									render : function(data, type, full, meta) {
										// message(data);
										var operate = "<input type='checkbox' class='unbindUserSelect' ownId='"
												+ data + "'/>";
										return operate;
									}
								}, {
									data : 'name'
								}, {
									data : 'sex'
								}, {
									data : 'hierarchy.groupName',
								}

						],
						"drawCallback" : function(oSettings) {
							var k = 0;
							var total = oSettings.aiDisplay.length
									+ oSettings._iDisplayStart;
							for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
								$(
										'td:eq(0)',
										oSettings.aoData[oSettings.aiDisplay[k]].nTr)
										.text(i + 1);
								k++;
							}
							serverUserUnbindHierarchyTable.off('click', 'tr');

						}
					});

};
// ===============================绑定组织的用户列表=============================
$(".searchBtn4").click(function() {

	serverUserBindHierarchyData.con = $("#search4").val();
	var hid = $("#hierarchyId").val();
	if (hid == '') {
		hid = '0';
	}
	serverUserBindHierarchyData.key = hid;

	serverUserBindHierarchyTable.draw();
});
$(".resetBtn4").click(function() {
	$("#search4").val('');
	serverUserBindHierarchyData.con = '';
	var hid = $("#hierarchyId").val();
	if (hid == '') {
		hid = '0';
	}
	serverUserBindHierarchyData.key = hid;
	serverUserBindHierarchyTable.draw();
});
var serverUserBindHierarchyData = {};
var serverUserBindHierarchyTable;

function showUserBindHierarchyPage(hierarchyId) {
	// 未绑定网格按钮全选
	$("#selectAllBindUser").click(function() {
		var state = $(this).prop("checked");
		$(".bindUserSelect").prop("checked", state);
	});

	serverUserBindHierarchyTable = $('#userBindHierarchyTable')
			.DataTable(
					{
						"serverSide" : true,
						"ordering" : false,
						"ajax" : {
							"dataType" : "json",
							"type" : "get",
							"cache" : false,
							"url" : window.rootPath
									+ '/ui/userGrid/showUserBindHierarchyPage',
							"data" : function(d) {
								return $.extend({}, d,
										serverUserBindHierarchyData);
							}
						},
						columns : [
								{
									data : null,
									defaultContent : ""
								},
								{
									data : 'id',
									defaultContent : "",
									render : function(data, type, full, meta) {
										var operate = "<input type='checkbox' class='bindUserSelect' ownId='"
												+ data + "'/>";
										return operate;
									}
								}, {
									data : 'name'
								}, {
									data : 'sex'
								}, {
									data : 'hierarchy.groupName',
								}

						],
						"drawCallback" : function(oSettings) {
							var k = 0;
							var total = oSettings.aiDisplay.length
									+ oSettings._iDisplayStart;
							for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
								$(
										'td:eq(0)',
										oSettings.aoData[oSettings.aiDisplay[k]].nTr)
										.text(i + 1);
								k++;
							}
							serverUserBindHierarchyTable.off('click', 'tr');

						}
					});

};

// ================================未绑定区域的网格列表=============================

$(".searchBtn1").click(function() {
	serverGridUnbindDivisionData.con = $("#search1").val();
	var divisionId = $("#gridAboutD").val();
	if ('' == divisionId) {
		divisionId = '0';
	}
	serverGridUnbindDivisionData.key = divisionId;
	serverGridUnbindDivisionTable.draw();
});
$(".resetBtn1").click(function() {
	$("#search1").val('');
	serverGridUnbindDivisionData.con = '';
	var divisionId = $("#gridAboutD").val();
	if ('' == divisionId) {
		divisionId = '0';
	}
	serverGridUnbindDivisionData.key = divisionId;
	serverGridUnbindDivisionTable.draw();
});
var serverGridUnbindDivisionData = {};
var serverGridUnbindDivisionTable;
function initGridUnbindDivision(id) {
	// 未绑定网格按钮全选
	$("#selectAllUnbindGrid").click(function() {
		var state = $(this).prop("checked");
		$(".unbindGridSelect").prop("checked", state);
	});

	serverGridUnbindDivisionTable = $('#gridUnbindDivisionTable')
			.DataTable(
					{
						"serverSide" : true,
						"ordering" : false,
						"ajax" : {
							"dataType" : "json",
							"type" : "post",
							"cache" : false,
							"url" : window.rootPath
									+ '/ui/userGrid/showGridUnbindDivisonPage',
							"data" : function(d) {
								return $.extend({}, d,
										serverGridUnbindDivisionData);
							}
						},
						columns : [
								{
									data : null,
									defaultContent : ""
								},
								{
									data : 'id',
									defaultContent : "",
									render : function(data, type, full, meta) {
										var operate = "<input type='checkbox' class='unbindGridSelect' ownId='"
												+ data + "'/>";
										return operate;
									}
								}, {
									data : 'gridIdentify'
								}, {
									data : 'gridName',
								}

						],
						"drawCallback" : function(oSettings) {
							var k = 0;
							var total = oSettings.aiDisplay.length
									+ oSettings._iDisplayStart;
							for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
								$(
										'td:eq(0)',
										oSettings.aoData[oSettings.aiDisplay[k]].nTr)
										.text(i + 1);
								k++;
							}
							serverGridUnbindDivisionTable.off('click', 'tr');

						}
					});

};
// ===============================绑定区域的网格列表=====================
$(".searchBtn2").click(function() {
	serverGridBindDivisionData.con = $("#search2").val();

	var divisionId = $("#gridAboutD").val();
	if ('' == divisionId) {
		divisionId = '0';
	}
	serverGridBindDivisionData.key = divisionId;
	serverGridBindDivisionTable.draw();
});
$(".resetBtn2").click(function() {
	$("#search2").val('');
	var divisionId = $("#gridAboutD").val();
	if ('' == divisionId) {
		divisionId = '0';
	}
	serverGridBindDivisionData.key = divisionId;
	serverGridBindDivisionData.con = '';
	serverGridBindDivisionTable.draw();
});
// 已绑定网格按钮全选
$("#selectAllbindGrid").click(function() {
	var state = $(this).prop("checked");
	$(".bindGridSelect").prop("checked", state);
});
var serverGridBindDivisionData = {};
var serverGridBindDivisionTable;
function showGridBindDivisonPage(divisionId) {
	serverGridBindDivisionTable = jQuery('#gridBindDivisionTable')
			.DataTable(
					{
						"serverSide" : true,
						"ordering" : false,
						"ajax" : {
							"dataType" : "json",
							"type" : "post",
							"cache" : false,
							"url" : window.rootPath
									+ '/ui/userGrid/showGridBindDivisonPage',
							"data" : function(d) {
								return $.extend({}, d,
										serverGridBindDivisionData);
							}
						},
						columns : [
								{
									data : null,
									defaultContent : ""
								},
								{
									data : 'id',
									defaultContent : "",
									render : function(data, type, full, meta) {
										var operate = "<input type='checkbox' class='bindGridSelect' ownId='"
												+ data + "'/>";
										return operate;
									}
								}, {
									data : 'gridIdentify'
								}, {
									data : 'gridName',
								}

						],
						"drawCallback" : function(oSettings) {
							var k = 0;
							var total = oSettings.aiDisplay.length
									+ oSettings._iDisplayStart;
							for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
								$(
										'td:eq(0)',
										oSettings.aoData[oSettings.aiDisplay[k]].nTr)
										.text(i + 1);
								k++;
							}
							serverGridBindDivisionTable.off('click', 'tr');

						}
					});
};

// ==============================组织树================================================
var hierarchySettings = {
	edit : {
		enable : true,
		showRemoveBtn : false,
		showRenameBtn : false,
		drag : {
			isCopy : false,
			isMove : false,
			prev : false,
			next : false,
			inner : false
		}
	},
	data : {
		simpleData : {
			enable : true,
			idKey : "id",
			pIdKey : "parentId",
			rootParentId : 0
		}
	},
	view : {
		showLine : true
	},
	check : {
		enable : true,
		chkStyle : "radio",
		radioType : "level"
	},

	callback : {
		onClick : initUserById
	}

};
var htreeNode = [];
function initHierarchyTree() {
	$.ajax({
		url : rootPath + "/ui/userGrid/initHierarchyTree",
		cache : false,
		type : "get",
		data : '',
		dataType : "json",
		contentType : 'application/json;charset=utf-8',// 解决返回数据中的中文乱码问题
		success : function(data) {
			htreeNode.push({
				id : 0,
				name : "组织层级",
				open : "true"
			// iconSkin:"diy03"
			});
			for (var i = 0; i < data.nodes.length; i++) {
				htreeNode.push(data.nodes[i]);
			}
			$.fn.zTree.init($("#hierarchyTree"), hierarchySettings, htreeNode);
		}
	});
};
function initUserById(e, treeId, treeNode) {
	var hierarchyId = treeNode.id;
	$("#hierarchyId").val(hierarchyId);
	// 初始化绑定组织的用户
	serverUserBindHierarchyData.key = hierarchyId;
	showUserBindHierarchyPage(hierarchyId);
	serverUserBindHierarchyTable.draw();

	// 初始化未绑定组织的用户
	serverUserUnbindHierarchyData.key = hierarchyId;
	initUserUnbindHierarchy(hierarchyId);
	serverUserUnbindHierarchyTable.draw();
};
// ===============================区域树==================================================
var divisionSettings = {
	edit : {
		enable : true,
		showRemoveBtn : false,
		showRenameBtn : false,
		drag : {
			isCopy : false,
			isMove : false,
			prev : false,
			next : false,
			inner : false
		}
	},
	data : {
		simpleData : {
			enable : true,
			idKey : "id",
			pIdKey : "parentId",
			rootParentId : 0
		}
	},
	view : {
		showLine : true
	},
	check : {
		enable : true,
		chkStyle : "radio",
		radioType : "level"
	},

	callback : {

		onClick : initGridByDivisonId
	}

};
// 初始化网格列表
function initGridByDivisonId(e, treeId, treeNode) {

	var divisionId = treeNode.id;
	$("#gridAboutD").val(divisionId);
	// 初始化该区域下已绑定的网格
	serverGridBindDivisionData.key = divisionId;
	showGridBindDivisonPage(divisionId);
	serverGridBindDivisionTable.draw();

	// 初始化该区域下尚未绑定的网格
	serverGridUnbindDivisionData.key = divisionId;
	initGridUnbindDivision(divisionId);
	serverGridUnbindDivisionTable.draw();
};
var dtreeNode = [];
function initDivisionTree() {
	$.ajax({
		url : rootPath + "/ui/userGrid/initDivisionTree",
		cache : false,
		type : "get",
		data : '',
		dataType : "json",
		contentType : 'application/json;charset=utf-8',// 解决返回数据中的中文乱码问题
		success : function(data) {
			dtreeNode.push({
				id : 0,
				name : "区域层级",
				open : "true"
			// iconSkin:"diy03"
			});
			for (var i = 0; i < data.nodes.length; i++) {
				dtreeNode.push(data.nodes[i]);
			}
			$.fn.zTree.init($("#divisionTree"), divisionSettings, dtreeNode);
		}
	});
};